﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Top.Web;
using Top.Data;
using Top.Business.OAuth;


namespace Top.PointStore.Business
{
    public static class DemoManager
    {
        public static async Task<List<S_Admin>> GetData(string name)
        {
            const string sql = "select  * from S_Admin where {0} is null or [F_RealName] like '%' + {0} + '%' limit 10";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, name).ToList<S_Admin>();
            }
        }


        public static object Get()
        {
            const string sql = "select * from S_Admin ";
            using (var db = new DbTour())
            {
                return db.Sql(sql).FirstOrDefault();
            }
        }

        public static async Task<object> UpdateMiss()
        {
            try
            {
                string sql = @"
                select F_UserKid from T_PointMain a
                left join T_OAuthInfo b on a.F_UserKid = b.F_BrokerKid
                where b.F_UserToken is null
                and F_UserType = 'T_Broker'
                and F_UserKid>1
                order by F_UserKid
                ";
                List<int> missUserKidList = new List<int>();
                List<T_OAuthInfo> oauthList = new List<T_OAuthInfo>();
                using (var db = new DbTour())
                {
                    missUserKidList = await db.Sql(sql).ToList<int>();
                }
                foreach (int kid in missUserKidList)
                {
                    string token = default(Guid).ToString();
                    try
                    {
                        token = OAuthHelper.GetTokenByBrokerKid(kid);// await OAuth.GetBrokerToken(kid);
                    }
                    catch
                    {
                        continue;
                    }
                    
                    oauthList.Add(new T_OAuthInfo()
                    {
                        F_BrokerKid = kid,
                        F_AdminKid = 0,
                        F_UserToken = token
                    });
                }
                foreach (T_OAuthInfo item in oauthList)
                {
                    OAuthInfoManager.AddOAuthInfo(item);
                }
                return 1;
            }
            catch (Exception ex)
            {                
                throw ex;
            }
           
        }
    }
}
